<?php
/**
 * @file trendycontroller.php
 * @author LinLin
 * @date 2016年6月15日 下午3:36:49
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\fashion;

use haibao\cms\business\control\ArticleController;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;
use \haibao\cms\business\control\pc\entity\ArticleEntity;
use \haibao\cms\business\control\pc\action\ArticleAction;

class TrendyController extends \haibao\cms\business\control\pc\channel\ChannelController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        $this->savaLog();
        $this->conditionTagId = 12204;
        parent::__construct($updateTime,$controlId,$isPublish);
        $this->articleAction = ArticleAction::initArticleAction();
    }

    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){

        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $parameterValue = array();
        if($controlData['auto']){
            $temp = array();
            foreach($controlData['auto'] as $data){
                $parameterValue[$data['Id']] = json_decode($data['ParameterValue'],true);
                $parameterValue[$data['Id']]['isRecommend'] = 1;
            }
           rsort($parameterValue);
           $parameterValue = $parameterValue[0];
        }
        $this->assign('trendyAuto', $parameterValue);
        $this->getTrendyData();

        $path = $this->generateHtmlPath(__CLASS__);
        $isNewHtml = $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }

    private function getTrendyData(){
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel(3);
        $articleEntity->setBigLevel(7);
        $articleEntity->setLimit(5);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(true);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(false);
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setTagIds($this->conditionTagId);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setProImageLevel(array(\haibao\cms\model\data\ArticleProImage::Level_FIVE));
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$articleProImages,$tagInfo,$articleTagArr) = $this->articleAction->getListForNew();
        
        $articleList = array();
        $articleLists = array();
        $this->tagInfo = $tagInfo;
        foreach($articles as $key=>$article){
            $articleList['TagInfo'] = $tagInfo[$article->ArticleTag->TagId];
            $articleList['ChannelUrl'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
            if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])){
                $article->ArticleProImage = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE];
            }else{
                if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
                    $article->ArticleProImage = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
                } else {
                    $article->ArticleProImage = \haibao\cms\business\control\PublicController::getArticleFirstImage($article->Id);
                }
            }
            $articleList['Article'] = $article;
            array_push($articleLists,$articleList);
        }
        $trendy = array();
        if($articleLists){
            foreach($articleLists as $key=>$val){
                $exclu['Id'] = $val['Article']->Id;
                $exclu['Title'] = isset($val['Article']->ShortTitle) ? $val['Article']->ShortTitle : $val['Article']->Title;
                $exclu['Url'] = $val['ChannelUrl'];
                $exclu['PublishRiqi']= $val['Article']->PublishTime->format('Y-m-d H:i:s');
                $exclu['Pic'] = $val['Article']->ArticleProImage;
                $trendy[] = $exclu;
            }
        }
        $trendy = array_slice($trendy,1,4,true);
        $this->assign('trendy', $trendy);
        $this->assign('calendarHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR));
    }


} 